package cn.iocoder.yudao.module.happycore.controller.admin.oxproject;

import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.param.CreateProjectParam;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.OxProjectPageReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.OxProjectRespVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.OxProjectSaveReqVO;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.PredictResponse;
import cn.iocoder.yudao.module.happycore.dal.dataobject.oxproject.OxProjectDO;
import cn.iocoder.yudao.module.happycore.service.PythonPredictService;
import cn.iocoder.yudao.module.happycore.service.PythonTrainService;
import cn.iocoder.yudao.module.happycore.service.oxproject.OxProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 项目")
@RestController
@RequestMapping("/happy/ox-project")
@Validated
public class OxProjectController {

    @Resource
    private OxProjectService oxProjectService;

    @Resource
    private PythonTrainService pythonTrainService;


    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('happy:ox-project:create')")
    public CommonResult<Long> createOxProject(@Valid @RequestBody OxProjectSaveReqVO createReqVO) {
        return success(oxProjectService.createOxProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('happy:ox-project:update')")
    public CommonResult<Boolean> updateOxProject(@Valid @RequestBody OxProjectSaveReqVO updateReqVO) {
        oxProjectService.updateOxProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('happy:ox-project:delete')")
    public CommonResult<Boolean> deleteOxProject(@RequestParam("id") Long id) {
        oxProjectService.deleteOxProject(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除项目")
                @PreAuthorize("@ss.hasPermission('happy:ox-project:delete')")
    public CommonResult<Boolean> deleteOxProjectList(@RequestParam("ids") List<Long> ids) {
        oxProjectService.deleteOxProjectListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('happy:ox-project:query')")
    public CommonResult<OxProjectRespVO> getOxProject(@RequestParam("id") Long id) {
        OxProjectDO oxProject = oxProjectService.getOxProject(id);
        return success(BeanUtils.toBean(oxProject, OxProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目分页")
    @PreAuthorize("@ss.hasPermission('happy:ox-project:query')")
    public CommonResult<PageResult<OxProjectRespVO>> getOxProjectPage(@Valid OxProjectPageReqVO pageReqVO) {
        PageResult<OxProjectDO> pageResult = oxProjectService.getOxProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OxProjectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('happy:ox-project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOxProjectExcel(@Valid OxProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OxProjectDO> list = oxProjectService.getOxProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目.xls", "数据", OxProjectRespVO.class,
                        BeanUtils.toBean(list, OxProjectRespVO.class));
    }
    @PostMapping("createProject")
    @Operation(summary = "创建项目")
    public CommonResult createProject(@RequestBody() @Validated CreateProjectParam createProjectParam) {

        OxProjectSaveReqVO oxProjectSaveReqVO = new OxProjectSaveReqVO();
        oxProjectSaveReqVO.setProjectName(createProjectParam.getProjectName());
        oxProjectSaveReqVO.setCategoryList(createProjectParam.getCategoryList());
        oxProjectSaveReqVO.setRemark(createProjectParam.getRemark());
        Long projectId = oxProjectService.createOxProject(oxProjectSaveReqVO);
        return success(projectId);
    }

    @PostMapping("/trainModel")
    @Operation(summary = "训练模型")
    public CommonResult<Boolean> trainModel(@RequestParam("projectId") Long projectId) {
        oxProjectService.trainModel(projectId);
        return success(true);
    }



}